//
//  MyhistoryProfit.m
//  NewPro
//
//  Created by 何荧红 on 2018/10/12.
//  Copyright © 2018 何荧红. All rights reserved.
//

#import "MyhistoryProfit.h"
#import "MyHistoryProfitCell.h"
#import "MyProfitStore.h"

@interface MyhistoryProfit ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ViewHeader *SecView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation MyhistoryProfit

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc]init];
    }
    return _dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initCSS];
    [self loadData];
}
- (void)loadData{
    [[[MyProfitStore alloc]init] GetMyHistoryProfitWithID:[UserDefaultsManager sharedInstance].user_id Success:^(NSDictionary * _Nonnull res) {
        if ([res[@"msg"] isEqualToString:@"success"]) {
            self.SecView.AccumulativemoneytString = [NSString stringWithFormat:@"%@",res[@"data"][@"sylj"]];
            for (NSDictionary *dic in res[@"data"][@"items"]) {
                [self.dataArr addObject:dic];
            }
        }else{
            [self showMBPError:res[@"msg"]];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    } Failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self showMBPError:[HttpTool handleError:error]];
    }];
}
- (void)initCSS{
    
    [self setNavBarWithTitle:@"历史收益"];
    
    self.SecView = [[ViewHeader alloc]initWithMyHistoryProfitSecView];
    [self.view addSubview:_SecView];
    
    [self.SecView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(120);
        make.left.right.top.equalTo(self.view);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.tableView registerClass:[MyHistoryProfitCell class] forCellReuseIdentifier:@"MyHistoryProfitCellID"];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (self.dataArr.count > 0) {
            [self.dataArr removeAllObjects];
        }
        [self loadData];
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.SecView.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 49;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    ViewHeader *header = [[ViewHeader alloc]initWithMyHistoryHeaderView];
    return header;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyHistoryProfitCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyHistoryProfitCellID"];
    if (self.dataArr.count > 0) {
        cell.subdic = self.dataArr[indexPath.row];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
@end
